<template>
  <div class="app-container">
    <!-- 查询对话框   -->
    <el-dialog
      title="查询"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="780px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="98px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="发生日期">
              <el-date-picker
                v-model="dateRange"
                size="small"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="daterange"
                style="width: 240px"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :clearable="false"
                :picker-options="pickerOptions"
                :default-time="['00:00:00', '23:59:59']"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料代码" prop="kcdWldm">
              <el-input
                v-model="queryParams.kcdWldm"
                placeholder="请输入物料代码"
                clearable
                style="width: 240px"
                size="small"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="客户编码" prop="itmCustwldm">
              <el-input
                v-model="queryParams.itmCustwldm"
                placeholder="请输入客户编码"
                clearable
                style="width: 240px"
                size="small"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单据类型" prop="idmDhzl">
              <el-select
                v-model="queryParams.idmDhzl"
                style="width: 240px"
                placeholder="请选择单据类型"
                clearable
                filterable
              >
                <el-option
                  v-for="item in idmMstrOptions"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="入库类型" prop="idmInDhzls">
              <el-select
                v-model="queryParams.idmInDhzls"
                multiple
                style="width: 240px"
                placeholder="请选择单据类型"
                clearable
                filterable
              >
                <el-option
                  v-for="item in idmMstrInOptions"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出库类型" prop="idmOutDhzls">
              <el-select
                v-model="queryParams.idmOutDhzls"
                multiple
                style="width: 240px"
                placeholder="请选择单据类型"
                clearable
                filterable
              >
                <el-option
                  v-for="item in idmMstrOutOptions"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="客户名称" prop="itmWldm">
              <el-select
                v-model="queryParams.itmWldm"
                style="width: 240px"
                placeholder="请选择客户名称"
                clearable
              >
                <el-option
                  v-for="item in customerOptions"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="仓库代码" prop="kcdCkdms">
              <el-select
                v-model="queryParams.kcdCkdms"
                multiple
                style="width: 240px"
                placeholder="请选择仓库"
                clearable
              >
                <el-option
                  v-for="item in storageOptions"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-col :span="24">
          <el-form-item label="综合查询" prop="itmPmgg">
            <el-input
              v-model="queryParams.itmPmgg"
              clearable
              type="textarea"
              style="width: 520px"
              size="small"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          @click="handleSearch"
          size="small"
          >查询</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          :loading="exportLoading1"
          v-hasPermi="['repertory:InOutDetail:export']"
          @click="exportDetailExcel()"
          size="small"
        >
          明细数据
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          :loading="exportLoading2"
          v-hasPermi="['repertory:InOutDetail:export']"
          @click="exportTotalExcel()"
          size="small"
        >
          汇总数据
        </el-button>
      </el-col>
    </el-row>

    <el-tabs v-model="activeTab">
      <el-tab-pane label="进出明细报表" name="first">
        <el-table
          border
          stripe
          id="inOutDetail"
          :height="tableHChild"
          :data="currentPageData"
          :loading="loading"
          :cell-style="styleCell"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column
            label="#"
            type="index"
            width="55"
            align="center"
            fixed="left"
          />
          <el-table-column
            label="物料代码"
            align="center"
            width="135"
            prop="vwldm"
          />
          <el-table-column
            label="发生日期"
            :show-overflow-tooltip="true"
            width="150"
            align="center"
            prop="vrq"
          >
            <template #default="scope">
              <span>{{
                parseTime(scope.row.vrq, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="客户编码"
            align="center"
            width="135"
            prop="vcustwldm"
          />
          <el-table-column
            label="规格描述"
            align="center"
            width="300"
            prop="vpmgg"
          />
          <el-table-column label="单位" align="center" width="60" prop="vdw" />
          <el-table-column
            label="前期库存"
            align="center"
            prop="vqqkc"
            width="100"
          />
          <el-table-column
            label="入库数量"
            align="center"
            prop="vrksl"
            width="100"
          />
          <el-table-column
            label="调入数量"
            align="center"
            prop="vtrsl"
            width="100"
          />
          <el-table-column
            label="出库数量"
            align="center"
            prop="vcksl"
            width="100"
          />
          <el-table-column
            label="结存数量"
            align="center"
            prop="vzzkc"
            width="100"
          />
          <el-table-column
            label="单据类型"
            align="center"
            prop="vdjType"
            width="100"
          />
          <el-table-column
            label="单据编号"
            align="center"
            prop="vdjbh"
            width="110"
          />
          <el-table-column
            label="仓库"
            align="center"
            prop="vckName"
            width="100"
          />
          <el-table-column
            label="存放位置"
            align="center"
            prop="vcwdm"
            width="120"
          />
          <el-table-column
            label="过账人员"
            align="center"
            prop="vjlry"
            width="100"
          />
        </el-table>
        <div style="float: right; margin: 5px 5px">
          <el-pagination
            layout="prev, pager, next"
            :page-size="pageSize"
            :total="total1"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </el-tab-pane>
      <el-tab-pane label="进出统计报表" name="second">
        <el-table
          border
          stripe
          id="inOutTotal"
          :height="tableHChild"
          :loading="loading"
          :data="currentPageData2"
          :cell-style="styleCell"
        >
          <el-table-column
            label="#"
            type="index"
            width="55"
            align="center"
            fixed="left"
          />
          <el-table-column
            label="物料代码"
            align="center"
            width="135"
            prop="vwldm"
          />
          <el-table-column
            label="客户代码"
            align="center"
            width="135"
            prop="vcustwldm"
          />
          <el-table-column
            label="规格描述"
            align="center"
            width="300"
            prop="vpmgg"
          />
          <el-table-column
            label="盘点数量"
            align="center"
            prop="vpdsl"
            width="100"
          />
          <el-table-column
            label="前期库存"
            align="center"
            prop="vqqkc"
            width="100"
          />
          <el-table-column
            label="仓库"
            align="center"
            prop="vckName"
            width="100"
          />
          <el-table-column
            label="入库总数"
            align="center"
            prop="vrksl"
            width="100"
          />
          <el-table-column
            label="调入总数"
            align="center"
            prop="vtrsl"
            width="100"
          />
          <el-table-column
            label="出库总数"
            align="center"
            prop="vcksl"
            width="100"
          />
          <el-table-column
            label="扣减数量"
            align="center"
            prop="vzjkc"
            width="100"
          />
          <el-table-column
            label="结存数量"
            align="center"
            prop="vzzkc"
            width="100"
          />
          <el-table-column
            label="即时库存"
            align="center"
            prop="vkcsl"
            width="100"
          />
        </el-table>
        <div style="float: right; margin: 5px 5px">
          <el-pagination
            layout="prev, pager, next"
            :page-size="pageSize2"
            :total="total2"
            @current-change="handleCurrentChange2"
          ></el-pagination>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { getAll, getstorageType } from "@/api/repertory/Storage";
import { getAllCustomer } from "@/api/project/customer";
import { getIdmMstrDicts, getIdmMstrDictsType } from "@/api/system/mstr";
import {
  inOutDetailList,
  inOutDetailReportExport,
  inOutTotalReportExport,
} from "@/api/repertory/InOutDetail";
import { exportTable } from "@/utils/ruoyi";

export default {
  name: "InOutDetail",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading1: false,
      exportLoading2: false,
      // 表格高度
      tableHChild: document.body.clientHeight - 250,
      // 首选项
      activeTab: "first",
      // 明细 OR Total
      // inOutDetailList: [],
      // inOutTotalList: [],
      // 显示搜索条件
      openSearch: false,
      // 总条数
      total1: 0,
      total2: 0,
      // 显示搜索条件
      showSearch: true,
      // 有效客户代码字典
      customerOptions: [],
      // 编号字典
      idmMstrOptions: [],
      //入库编号字典编号字典
      idmMstrInOptions: [],
      //出库编号字典
      idmMstrOutOptions: [],
      //有效存储仓库
      storageOptions: [],
      //所有存储储位
      allCwOptions: [],
      // 单位字典
      unitFlagOptions: [],
      // 建立日期时间范围
      dateRange: [
        this.$moment().startOf("month").toDate(),
        this.$moment().endOf("month").toDate(),
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 40,
        kcdWldm: null,
        itmCustwldm: null,
        itmPmgg: null,
        idmDhzl: null,
        idmOutDhzls: [],
        idmInDhzls: [],
        itmWldm: null,
        kcdCkdms: [],
      },
      // 表单参数
      form: {},
      //日期初始化
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      //新分页参数
      pageSize: 25,
      pageSize2: 25,
      total: 50,
      currentPage: 1,
      currentPageData: [],
      currentPage2: 1,
      currentPageData2: [],
    };
  },
  created() {
    // this.getList()
    // 编号管理字典
    getIdmMstrDicts().then((res) => {
      for (let i of res.data) {
        i.dictLabel = "[" + i.idmDhzl + "]" + i.idmDesc;
        i.dictValue = i.idmDhzl;
        i.listClass = "default";
      }
      this.idmMstrOptions = res.data;
    });

    getIdmMstrDictsType("CKM").then((res) => {
      for (let i of res.data) {
        i.dictLabel = "[" + i.idmDhzl + "]" + i.idmDesc;
        i.dictValue = i.idmDhzl;
        i.listClass = "default";
      }
      this.idmMstrOutOptions = res.data;
    });
    getIdmMstrDictsType("RKM").then((res) => {
      for (let i of res.data) {
        i.dictLabel = "[" + i.idmDhzl + "]" + i.idmDesc;
        i.dictValue = i.idmDhzl;
        i.listClass = "default";
      }
      this.idmMstrInOptions = res.data;
    });
    //获取有效客户
    getAllCustomer().then((response) => {
      for (let i of response.data) {
        i.dictLabel = "[" + i.cusCode + "]" + i.cusName;
        i.dictValue = i.cusCode;
        i.listClass = "default";
      }
      this.customerOptions = response.data;
    });
    //单位
    // this.getDicts('sys_unit').then((response) => {
    //   this.unitFlagOptions = response.data
    //   this.unitFlagOptions.push({ dictLabel: '小计', dictValue: '小计', listClass: 'default' })
    // })
    getstorageType("0").then((response) => {
      this.storageOptions = response.data;

      for (let item of this.storageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });
    // getAll().then((response) => {
    //   this.allCwOptions = response.data
    //
    //   for (let item2 of this.allCwOptions) {
    //     item2.dictLabel = item2.storageDefaultstorage
    //
    //     item2.listClass = 'default'
    //     item2.dictValue = item2.storageId + ''
    //   }
    // })
  },
  methods: {
    /** 查询来料进货入库开单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      this.queryParams.pageNum = 1;
      if (null != this.dateRange && "" != this.dateRange) {
        this.queryParams.params["beginKcdRq"] = this.moment(
          this.dateRange[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endKcdRq"] = this.moment(
          this.dateRange[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      inOutDetailList(this.queryParams).then((response) => {
        //明细
        this.inOutDetailList = response.data.list1;
        this.total1 = this.inOutDetailList.length;
        const start = (this.currentPage - 1) * this.pageSize;
        const end = start + this.pageSize;
        this.currentPageData = this.inOutDetailList.slice(start, end);
        //统计
        this.inOutTotalList = response.data.list2;
        this.total2 = this.inOutTotalList.length;
        const start2 = (this.currentPage2 - 1) * this.pageSize2;
        const end2 = start + this.pageSize2;
        this.currentPageData2 = this.inOutTotalList.slice(start2, end2);
        this.loading = false;
      });
    },
    handleSearch() {
      this.openSearch = true;
    },
    // 取消按钮
    cancel() {
      this.openSearch = false;
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.inOutDetailList = [];
      this.inOutTotalList = [];
      this.getList();
      this.openSearch = false;
    },
    //导出Excel
    handleExcel(a, b) {
      exportTable(a, b);
    },
    /** 导出明细数据 */
    exportDetailExcel() {
      this.queryParams.params = {};
      if (null != this.dateRange && "" != this.dateRange) {
        this.queryParams.params["beginKcdRq"] = this.moment(
          this.dateRange[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endKcdRq"] = this.moment(
          this.dateRange[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      const queryParams = this.queryParams;
      this.$confirm("确认要导出仓库进出明细数据？", "确认", {
        cancelButtonText: "取消",
        confirmButtonText: "确认",
        type: "info",
      })
        .then(() => {
          this.exportLoading1 = true;
          return inOutDetailReportExport(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading1 = false;
        })
        .catch(() => {});
    },
    /** 导出汇总数据 */
    exportTotalExcel() {
      this.queryParams.params = {};
      if (null != this.dateRange && "" != this.dateRange) {
        this.queryParams.params["beginKcdRq"] = this.moment(
          this.dateRange[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endKcdRq"] = this.moment(
          this.dateRange[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      const queryParams = this.queryParams;
      this.$confirm("确认要导出仓库进出汇总数据？", "确认", {
        cancelButtonText: "取消",
        confirmButtonText: "确认",
        type: "info",
      })
        .then(() => {
          this.exportLoading2 = true;
          return inOutTotalReportExport(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading2 = false;
        })
        .catch(() => {});
    },
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    styleCell({ row, column, rowIndex, columnIndex }) {
      if (row[column.property] && row[column.property] < 0)
        return { color: "red" };
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      const start = (this.currentPage - 1) * this.pageSize;
      const end = start + this.pageSize;
      this.currentPageData = this.inOutDetailList.slice(start, end);
    },
    handleCurrentChange2(val) {
      this.currentPage2 = val;
      const start = (this.currentPage2 - 1) * this.pageSize2;
      const end = start + this.pageSize2;
      this.currentPageData2 = this.inOutTotalList.slice(start, end);
    },
  },
};
</script>

<style lang="scss" scoped></style>
